"use strict";
cc._RF.push(module, '253167DlHdBSrutAbKKUB33', 'GameDraw');
// scripts/GameDraw.ts

Object.defineProperty(exports, "__esModule", { value: true });
var Config_1 = require("./Config");
var LineLogic_1 = require("./LineLogic");
var Model_1 = require("./Model");
var MetaballMgr_1 = require("./MetaballMgr");
var Ball_1 = require("./Ball");
var TipLine_1 = require("./TipLine");
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var GameDraw = /** @class */ (function (_super) {
    __extends(GameDraw, _super);
    function GameDraw() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.Ball = null;
        _this.TopUI = null;
        _this.BottomUI = null;
        _this.Cup = null;
        _this.Line = null;
        _this.metaBall = null;
        _this.lock = false;
        _this.waterPipe = null;
        _this.progressNode = null;
        _this.waterContainer = null;
        _this.SmokeEff = null;
        _this.winClip = null;
        _this.waterClip = null;
        _this.allObjs = [];
        _this.totalInk = 40;
        _this.totalWater = 50;
        _this.waterCount = 0;
        _this.waters = [];
        _this.faceChange = false;
        _this.linePosY = 0;
        //记录动态的物体，画线后才变动态，之前为静态
        _this.dynamicObjs = [];
        _this.redObjs = [];
        //在水杯中的水滴数量
        _this.cupInWaterObj = {};
        _this.cupWater = 0;
        _this.tip = null;
        //已经绘制的长度，由于可以绘制多条
        _this.hasDrawLen = 0;
        _this.canMove = true;
        _this.waterSoundDelay = 0.2;
        _this.waterSoundDelayCount = 10;
        return _this;
    }
    GameDraw.prototype.onLoad = function () {
        Config_1.default.init();
        Model_1.default.game.selectedModel = 4;
        //开启物理效果
        cc.director.getPhysicsManager().enabled = true;
        cc.director.getPhysicsManager().gravity = new cc.Vec2(0, -1200);
        cc.director.getActionManager()['enabledAccumulator'] = true;
        cc.director.getActionManager()['FIXED_TIME_STEP'] = 1 / 30;
        cc.director.getPhysicsManager()['VELOCITY_ITERATIONS'] = 8;
        // cc.director.getPhysicsManager().debugDrawFlags = cc.PhysicsManager.DrawBits.e_jointBit | cc.PhysicsManager.DrawBits.e_shapeBit;
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        var size = cc.view.getVisibleSize();
        if (size.height / size.width > 2) {
            this.progressNode.getComponent(cc.Widget).top = 200;
        }
        else {
            this.progressNode.getComponent(cc.Widget).top = 145;
        }
        this.createWorld();
        var top = cc.instantiate(this.TopUI);
        this.node.addChild(top);
        this.metaBall.getComponent(MetaballMgr_1.default).waters = this.waters;
        var bottom = cc.instantiate(this.BottomUI);
        this.node.addChild(bottom);
        bottom.on('showTip', this.showTip, this);
        //开启抗锯齿
        // cc.macro.ENABLE_WEBGL_ANTIALIAS=true;
        this.progressNode.zIndex = 100;
        this.tip.zIndex = 100;
        this.node.getChildByName('gameTip').active = Model_1.default.game.selectedLevel == 1;
    };
    GameDraw.prototype.createWorld = function () {
        var _this = this;
        //createCup
        var level = Model_1.default.game.selectedLevel;
        var data = Config_1.default.drawLevels[level];
        var cupPos = data[0];
        var cup = cc.instantiate(this.Cup);
        cup.getComponent(cc.RigidBody).type = cc.RigidBodyType.Static;
        cup.x = cupPos[0];
        cup.y = cupPos[1];
        this.waterContainer.addChild(cup);
        this.cup = cup;
        if (cupPos.length > 2) {
            cup.angle = -cupPos[2];
        }
        var cupData = Config_1.default.cupDatas[1];
        // for (var i = 0; i < cupData.polygonColliderList.length; i++) {
        //     var collider = cup.addComponent(cc.PhysicsPolygonCollider)
        //     collider.points = cupData.polygonColliderList[i]
        //     collider.density = 10
        //     collider.apply()
        // }
        cup.getComponent(cc.RigidBody).enabledContactListener = true;
        cup.getComponent(cc.RigidBody).onBeginContact = function (contact, selfCollider, otherCollider) {
            _this.onBeginContact(contact, selfCollider, otherCollider);
        };
        this.linePosY = cupData.linePosY + cup.y;
        //添加一个传感器用于检测水杯中的水滴
        var collider = this.cup.addComponent(cc.PhysicsPolygonCollider);
        this.colliderPoints = [cc.v2(-43, -82), cc.v2(43, -82), cc.v2(72, 70), cc.v2(-69, 70)];
        collider.points = this.colliderPoints;
        collider.sensor = true;
        collider.apply();
        //水龙头
        var pipeInfo = data[1];
        this.waterPipe.x = pipeInfo[0];
        this.waterPipe.y = pipeInfo[1];
        this.waterPipe.angle = -pipeInfo[2];
        this.totalInk = data[3];
        this.totalWater = data[4];
        //平台
        for (var i = 5; i < data.length; i++) {
            var brickType = data[i][0];
            var brick = cc.instantiate(this.allObjs[brickType - 1]);
            brick.x = data[i][1];
            brick.y = data[i][2];
            if (brickType == 3 || brickType == 8 || brickType == 11 || brickType == 13) //矩形
             {
                brick.setContentSize(cc.size(data[i][3], data[i][4]));
                brick.getComponent(cc.PhysicsBoxCollider).size = cc.size(cc.size(data[i][3], data[i][4]));
                brick.getComponent(cc.PhysicsBoxCollider).apply();
                //火焰特殊处理
                if (brickType == 11) {
                    brick.getComponent(cc.RigidBody).onBeginContact = function (contact, selfCollider, otherCollider) {
                        _this.onBeginContact(contact, selfCollider, otherCollider);
                    };
                }
                if (brickType == 13) {
                    var size = brick.getComponent(cc.PhysicsBoxCollider).size;
                    var points = [cc.v2(brick.x - size.width * 0.5, brick.y - size.height * 0.5),
                        cc.v2(brick.x + size.width * 0.5, brick.y - size.height * 0.5),
                        cc.v2(brick.x + size.width * 0.5, brick.y + size.height * 0.5),
                        cc.v2(brick.x - size.width * 0.5, brick.y + size.height * 0.5)];
                    this.redObjs.push(points);
                }
            }
            else if (brickType == 7 || brickType == 4) //圆
             {
                brick.setContentSize(cc.size(data[i][3], data[i][4]));
                brick.getComponent(cc.PhysicsCircleCollider).radius = data[i][3] * 0.5;
                brick.getComponent(cc.PhysicsCircleCollider).apply();
            }
            brick.angle = -data[i][5];
            if (brickType == 13)
                this.waterContainer.addChild(brick, -1);
            else
                this.waterContainer.addChild(brick);
            if (brick.getComponent(cc.RigidBody) != null) {
                if (brick.getComponent(cc.RigidBody).type == cc.RigidBodyType.Dynamic) {
                    this.dynamicObjs.push(brick);
                }
                brick.getComponent(cc.RigidBody).type = cc.RigidBodyType.Static;
            }
            if (Model_1.default.game.selectedLevel == 32 && brickType == 6) //特殊处理下这个球
             {
                brick.getComponent(cc.PhysicsCircleCollider).density = 0.01;
                brick.getComponent(cc.PhysicsCircleCollider).apply();
            }
        }
    };
    GameDraw.prototype.start = function () {
        //提示
        var level = Model_1.default.game.selectedLevel;
        var data = Config_1.default.drawLevels[level];
        this.tip.getComponent(TipLine_1.default).points = data[2];
        this.tip.active = false;
    };
    GameDraw.prototype.checkInRed = function (pos) {
        var size = cc.view.getVisibleSize();
        for (var _i = 0, _a = this.redObjs; _i < _a.length; _i++) {
            var temp = _a[_i];
            if (cc.Intersection.pointInPolygon(cc.v2(pos.x - size.width * 0.5, pos.y - size.height * 0.5), temp)) {
                console.log('lllllllllllllllllllll');
                return true;
            }
        }
        return false;
    };
    GameDraw.prototype.onTouchStart = function (e) {
        if (this.lock)
            return;
        var pos = e.getLocation();
        if (this.checkInRed(pos)) {
            this.canMove = false;
            return;
        }
        else {
            this.canMove = true;
        }
        if (this.currentLine != null) {
            this.hasDrawLen += this.currentLine.getComponent(LineLogic_1.default).points.length;
        }
        var line = cc.instantiate(this.Line);
        this.node.addChild(line);
        this.currentLine = line;
        this.currentLine.getComponent(LineLogic_1.default).onTouchStart(e);
        return true;
    };
    GameDraw.prototype.onTouchMove = function (e) {
        if (!this.canMove)
            return;
        var totalLen = this.currentLine.getComponent(LineLogic_1.default).points.length + this.hasDrawLen;
        if (totalLen <= this.totalInk) {
            this.currentLine.getComponent(LineLogic_1.default).onTouchMove(e);
            var p = (this.totalInk - totalLen) / this.totalInk;
            this.progressNode.getComponent(cc.ProgressBar).progress = p;
            this.progressNode.getChildByName('progress_label').getComponent(cc.Label).string = Math.floor(p * 100) + '%';
            if (p < 0.8) {
                this.progressNode.getChildByName('star3').active = false;
                this.progressNode.getChildByName('stargray3').active = true;
            }
            if (p < 0.6) {
                this.progressNode.getChildByName('star2').active = false;
                this.progressNode.getChildByName('stargray2').active = true;
            }
            if (p < 0.1) {
                this.progressNode.getChildByName('star1').active = false;
                this.progressNode.getChildByName('stargray1').active = true;
            }
        }
    };
    GameDraw.prototype.onTouchEnd = function (e) {
        if (!this.canMove)
            return;
        this.currentLine.getComponent(LineLogic_1.default).onTouchEnd(e);
        this.cup.getComponent(cc.RigidBody).type = cc.RigidBodyType.Dynamic;
        for (var _i = 0, _a = this.dynamicObjs; _i < _a.length; _i++) {
            var obj = _a[_i];
            obj.getComponent(cc.RigidBody).type = cc.RigidBodyType.Dynamic;
        }
        this.startDropWater();
    };
    //开始滴水
    GameDraw.prototype.startDropWater = function () {
        console.log('开始创建水');
        this.schedule(this.createWater, 0.06);
    };
    GameDraw.prototype.createWater = function () {
        if (this.waterCount < this.totalWater) {
            this.waterCount++;
            var ball = cc.instantiate(this.Ball);
            this.waterSoundDelayCount += 0.06;
            if (this.waterSoundDelayCount > this.waterSoundDelay) {
                this.waterSoundDelayCount = 0;
                cc.audioEngine.playEffect(this.waterClip, false);
            }
            // console.log(this.waterPipe.rotation)
            if (this.waterPipe.angle == 0) {
                ball.x = this.waterPipe.x + Math.random() * 8 - 16;
                ball.y = this.waterPipe.y + Math.random() * 5 - 75;
                ball.getComponent(cc.RigidBody).linearVelocity = cc.v2(0, -0);
            }
            else if (this.waterPipe.angle >= 181 && this.waterPipe.angle <= 178) {
                ball.x = this.waterPipe.x + Math.random() * 8 - 16;
                ball.y = this.waterPipe.y + Math.random() * 5 + 75;
                ball.getComponent(cc.RigidBody).linearVelocity = cc.v2(0, 100);
            }
            else {
                var center = this.cup.getComponent(cc.RigidBody).getWorldCenter();
                var sign = this.waterPipe.angle < 0 ? -1 : 1;
                ball.x = this.waterPipe.x + 60 * sign + Math.random() * 8 - 4;
                ball.y = this.waterPipe.y + Math.random() * 10 - 5;
                ball.getComponent(cc.RigidBody).linearVelocity = cc.v2(sign * 150, 0);
                // ball.getComponent(cc.RigidBody).applyLinearImpulse(cc.v2(200000,200000), cc.v2(center.x, center.y), true);
                // ball.getComponent(cc.RigidBody).applyForce(cc.v2(1000,0),center,true)
            }
            ball.getComponent(Ball_1.default).id = this.waterCount;
            ball.getComponent(cc.PhysicsCircleCollider).density = 1;
            ball.getComponent(cc.PhysicsCircleCollider).restitution = 0.2;
            ball.getComponent(cc.PhysicsCircleCollider).apply();
            this.waterContainer.addChild(ball, -1);
            this.waters.push(ball);
        }
        else {
            this.unschedule(this.createWater);
            this.schedule(this.checkGame, 0.1);
        }
    };
    GameDraw.prototype.showFace = function (type) {
        this.cup.getChildByName('facenormal').active = false;
        this.cup.getChildByName('facewin').active = false;
        this.cup.getChildByName('facelose').active = false;
        if (type == 2) {
            this.cup.getChildByName('facewin').active = true;
        }
        else if (type == 1) {
            this.cup.getChildByName('facelose').active = true;
        }
        else if (type == 0) {
            this.cup.getChildByName('facenormal').active = true;
        }
    };
    GameDraw.prototype.update = function (dt) {
        if (this.waterCount > 0) {
            this.metaBall.getComponent(MetaballMgr_1.default).draw();
            for (var i = 0; i < this.waters.length; i++) {
                if (this.waters[i].y < -1000) {
                    this.waters.splice(i, 1);
                    i--;
                }
            }
        }
    };
    GameDraw.prototype.checkGame = function () {
        if (this.lock)
            return;
        //检测胜利
        if (this.waters.length >= 39) {
            var newRect = [];
            for (var _i = 0, _a = this.colliderPoints; _i < _a.length; _i++) {
                var a = _a[_i];
                newRect.push(cc.v2(this.cup.x + a.x, a.y + this.cup.y));
            }
            var count = 0;
            for (var _b = 0, _c = this.waters; _b < _c.length; _b++) {
                var temp = _c[_b];
                if (cc.Intersection.pointInPolygon(temp.position, newRect)) {
                    count++;
                }
            }
            //console.log(count);
            if (count >= 39) {
                this.gameWin();
            }
        }
        //失败检测
        this.checkLose();
    };
    GameDraw.prototype.checkLose = function () {
        if (this.waters.length <= 15) {
            this.gameLose();
            return;
        }
        var allSleep = true;
        for (var _i = 0, _a = this.waters; _i < _a.length; _i++) {
            var ball = _a[_i];
            if (ball.getComponent(cc.RigidBody).awake) {
                allSleep = false;
                break;
            }
        }
        if (allSleep) {
            this.gameLose();
        }
    };
    GameDraw.prototype.gameWin = function () {
        if (this.lock)
            return;
        this.unscheduleAllCallbacks();
        this.lock = true;
        Model_1.default.game.win = true;
        this.showFace(2);
        this.scheduleOnce(this.delayShowEnd, 1);
        cc.audioEngine.playEffect(this.winClip, false);
        this.saveData();
    };
    GameDraw.prototype.gameLose = function () {
        if (this.lock)
            return;
        this.unscheduleAllCallbacks();
        this.lock = true;
        Model_1.default.game.win = false;
        this.showFace(1);
        this.scheduleOnce(this.delayShowEnd, 1);
    };
    GameDraw.prototype.delayShowEnd = function () {
        cc.director.loadScene('gameEnd');
    };
    GameDraw.prototype.showTip = function () {
        console.log('显示提示');
        this.tip.active = true;
    };
    GameDraw.prototype.onBeginContact = function (contact, selfCollider, otherCollider) {
        // console.log(selfCollider.node.name, otherCollider.node.name)
        if ((selfCollider instanceof cc.PhysicsBoxCollider) && selfCollider.node.name == 'Cup3' && otherCollider.node.name == 'Ball') {
            //selfCollider.getAABB()
            // if (this.cupInWaterObj[otherCollider.node.getComponent(Ball).id] == null) {
            //     this.cupInWaterObj[otherCollider.node.getComponent(Ball).id] = true
            //     this.cupWater++
            //     // console.log(this.cupWater,'aaaaa')
            //     if (this.cupWater >= 30) {
            //         this.gameWin()
            //     }
            // }
            if (!this.faceChange) {
                this.faceChange = true;
                this.showFace(1);
            }
        }
        //火焰
        if (selfCollider.node.name == 'r11' && otherCollider.node.name == 'Ball') {
            for (var i = 0; i < this.waters.length; i++) {
                if (this.waters[i] == otherCollider.node) {
                    this.waters.splice(i, 1);
                    i--;
                    this.createSmokeEff(otherCollider.node.x, otherCollider.node.y);
                    otherCollider.node.destroy();
                    break;
                }
            }
        }
    };
    GameDraw.prototype.saveData = function () {
        var star = 0;
        if (this.progressNode.getChildByName('star1').active)
            star++;
        if (this.progressNode.getChildByName('star2').active)
            star++;
        if (this.progressNode.getChildByName('star3').active)
            star++;
        Model_1.default.game.getStar = star;
        //存储
        if (Model_1.default.game.selectedLevel == Model_1.default.game.drawLevel) {
            Model_1.default.game.drawLevelInfo[Model_1.default.game.selectedLevel - 1] = star;
            Model_1.default.game.drawLevelInfo[Model_1.default.game.selectedLevel] = -1;
            Model_1.default.game.drawLevel = Model_1.default.game.selectedLevel + 1;
            Model_1.default.game.saveDraw();
        }
        else {
            if (star > Model_1.default.game.drawLevelInfo[Model_1.default.game.selectedLevel - 1]) {
                Model_1.default.game.drawLevelInfo[Model_1.default.game.selectedLevel - 1] = star;
                Model_1.default.game.saveDraw();
            }
            //  Model.game.selectedLevel++
        }
    };
    GameDraw.prototype.createSmokeEff = function (x, y) {
        var eff = cc.instantiate(this.SmokeEff);
        eff.x = x;
        eff.y = y;
        this.node.addChild(eff);
    };
    GameDraw.prototype.checkFace = function () {
    };
    __decorate([
        property(cc.Prefab)
    ], GameDraw.prototype, "Ball", void 0);
    __decorate([
        property(cc.Prefab)
    ], GameDraw.prototype, "TopUI", void 0);
    __decorate([
        property(cc.Prefab)
    ], GameDraw.prototype, "BottomUI", void 0);
    __decorate([
        property(cc.Prefab)
    ], GameDraw.prototype, "Cup", void 0);
    __decorate([
        property(cc.Prefab)
    ], GameDraw.prototype, "Line", void 0);
    __decorate([
        property(cc.Node)
    ], GameDraw.prototype, "metaBall", void 0);
    __decorate([
        property(cc.Node)
    ], GameDraw.prototype, "waterPipe", void 0);
    __decorate([
        property(cc.Node)
    ], GameDraw.prototype, "progressNode", void 0);
    __decorate([
        property(cc.Node)
    ], GameDraw.prototype, "waterContainer", void 0);
    __decorate([
        property(cc.Prefab)
    ], GameDraw.prototype, "SmokeEff", void 0);
    __decorate([
        property({ type: cc.AudioClip })
    ], GameDraw.prototype, "winClip", void 0);
    __decorate([
        property({ type: cc.AudioClip })
    ], GameDraw.prototype, "waterClip", void 0);
    __decorate([
        property([cc.Prefab])
    ], GameDraw.prototype, "allObjs", void 0);
    __decorate([
        property(cc.Node)
    ], GameDraw.prototype, "tip", void 0);
    GameDraw = __decorate([
        ccclass
    ], GameDraw);
    return GameDraw;
}(cc.Component));
exports.default = GameDraw;

cc._RF.pop();